package com.yanqu.road.dao.impl.channelcenter.tonbingchannel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.tonbingchannel.data.ChannelTonBingCampBuff;
import com.yanqu.road.entity.activity.tonbingchannel.data.ChannelTonBingCity;
import com.yanqu.road.entity.activity.tonbingchannel.data.ChannelTonBingCityEvent;
import com.yanqu.road.entity.activity.tonbingchannel.data.ChannelTonBingData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ChannelTonBingDataDaoImpl extends TempDataObjectDao<ChannelTonBingData> {


    public int getMaxMatchId(int activityId) {
        String sql = "select max(matchId) as maxMatchId from t_n_tonbing_data where activityId=?";
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getChannelServerDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                int data = rs.getInt("maxMatchId");
                if(data == 0){
                    return 1;
                }
                return data + 1;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return 1;
    }

    public static String getChannelTonBingDataSql = "select * from `t_n_tonbing_data` where `activityId`=?";

    public Map<Integer,ChannelTonBingData> getChannelTonBingDataMap(int activityId) {
        Map<Integer,ChannelTonBingData> resMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getChannelServerDb());
            call = getDb().getConnection().prepareCall(getChannelTonBingDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ChannelTonBingData data = parse(rs);
                resMap.put(data.getMatchId(),data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return resMap;
    }

    public static String updateChannelTonBingDataSql = "update `t_n_tonbing_data` set " +
            "lockUnion=?,process2WinUserId=?,refreshCityEventTime=?,applyCityEventTime=?,cityEventMap=?," +
            "campBuffMap=?,npcEventAddTime=?,cityMap=?,autoSendCollect=?,unionIdList=?,rankBuffMap=? where activityId=? AND matchId=?";

    @Override
    protected boolean updateData(ChannelTonBingData crossTonBingData) {
        crossTonBingData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getChannelServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BOOLEAN, crossTonBingData.isLockUnion()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTonBingData.getProcess2WinUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTonBingData.getRefreshCityEventTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTonBingData.getApplyCityEventTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingData.getCityEventMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingData.getCampBuffMap())));
        para.put(i++, new DbParameter(Types.BIGINT, crossTonBingData.getNpcEventAddTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingData.getCityMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossTonBingData.getAutoSendCollect()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingData.getUnionIdList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingData.getRankBuffMap())));

        para.put(i++, new DbParameter(Types.INTEGER, crossTonBingData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossTonBingData.getMatchId()));
        if (getDb().executeNoneQuery(updateChannelTonBingDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertChannelTonBingDataSql = "insert into `t_n_tonbing_data`" +
            "(activityId,lockUnion,process2WinUserId,refreshCityEventTime,applyCityEventTime," +
            "cityEventMap,campBuffMap,npcEventAddTime,cityMap,autoSendCollect,matchId,turn,warType,unionIdList,rankBuffMap) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(ChannelTonBingData crossTonBingData) {
        crossTonBingData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getChannelServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossTonBingData.getActivityId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, crossTonBingData.isLockUnion()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTonBingData.getProcess2WinUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTonBingData.getRefreshCityEventTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTonBingData.getApplyCityEventTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingData.getCityEventMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingData.getCampBuffMap())));
        para.put(i++, new DbParameter(Types.BIGINT, crossTonBingData.getNpcEventAddTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingData.getCityMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossTonBingData.getAutoSendCollect()));
        para.put(i++, new DbParameter(Types.INTEGER, crossTonBingData.getMatchId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossTonBingData.getTurn()));
        para.put(i++, new DbParameter(Types.INTEGER, crossTonBingData.getWarType()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingData.getUnionIdList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTonBingData.getRankBuffMap())));
        if (getDb().executeNoneQuery(insertChannelTonBingDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected ChannelTonBingData parseData(ResultSet rs) throws SQLException {
        ChannelTonBingData crossTonBingData = new ChannelTonBingData();
        crossTonBingData.setActivityId(rs.getInt("activityId"));
        crossTonBingData.setLockUnion(rs.getBoolean("lockUnion"));
        crossTonBingData.setProcess2WinUserId(rs.getLong("process2WinUserId"));
        crossTonBingData.setRefreshCityEventTime(rs.getLong("refreshCityEventTime"));
        crossTonBingData.setApplyCityEventTime(rs.getLong("applyCityEventTime"));
        crossTonBingData.setCityEventMap(JSON.parseObject(rs.getString("cityEventMap"), new TypeReference<ConcurrentHashMap<Integer, ChannelTonBingCityEvent>>() {
        }));
        crossTonBingData.setCampBuffMap(JSON.parseObject(rs.getString("campBuffMap"), new TypeReference<ConcurrentHashMap<Integer, List<ChannelTonBingCampBuff>>>() {
        }));
        crossTonBingData.setNpcEventAddTime(rs.getLong("npcEventAddTime"));
        crossTonBingData.setCityMap(JSON.parseObject(rs.getString("cityMap"), new TypeReference<ConcurrentHashMap<Integer, ChannelTonBingCity>>() {
        }));
        crossTonBingData.setAutoSendCollect(rs.getInt("autoSendCollect"));
        crossTonBingData.setMatchId(rs.getInt("matchId"));
        crossTonBingData.setTurn(rs.getInt("turn"));
        crossTonBingData.setWarType(rs.getInt("warType"));
        crossTonBingData.setUnionIdList(JSON.parseObject(rs.getString("unionIdList"), new TypeReference<List<String>>() {
        }));
        crossTonBingData.setRankBuffMap(JSON.parseObject(rs.getString("rankBuffMap"), new TypeReference<ConcurrentHashMap<Integer,Integer>>() {
        }));
        return crossTonBingData;
    }

}
